//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#include <bsp.h>
#include "uart.h"
#include "nu_api.h"


EXTERN_C bool_t CDECL BspSetupComPort(
    ComPort comPort, const ComPortSettings *pSettings)
{
    // Do nothing here.
    return TRUE;
}

EXTERN_C bool_t CDECL BspWriteComPort(ComPort comPort, uint8_t u8Data)
{
#ifndef ELASTOS_RC
    nu_uart_sendData(&u8Data, 1);
#endif
    return TRUE;
}


typedef _ELASTOS Int32 WaitResult;
EXTERN void DzSleep(uint_t uTicks, WaitResult *pResult);


EXTERN_C void NotifyByIsr(int eType, int nCode, void *pParam);
EXTERN_C bool_t CDECL BspReadComPort(ComPort comPort, uint8_t *pu8Data)
{
    UInt32 numDataRead;
    static unsigned char    s_uart_buffer[64];
    static unsigned char    *s_start = s_uart_buffer;
    static unsigned long    s_numUsed;

    if (NULL == pu8Data) return FALSE;

    if (!COMPORT_AVAILABLE(comPort)) return FALSE;

    do {
        if (s_numUsed == 0) {
            numDataRead = nu_uart_receiveData(s_uart_buffer, 64);
            if (numDataRead) {
                *pu8Data    = s_uart_buffer[0];
                s_start     = s_uart_buffer + 1;
                s_numUsed   = numDataRead - 1;
                return TRUE;
            }
        }
        else {
            *pu8Data = *s_start;
            s_start++;
            s_numUsed--;
            return TRUE;
        }
        DzSleep(1, NULL);
    } while (1);

    return FALSE;
}

EXTERN_C bool_t CDECL TryReadComPort(
    ComPort comPort, uint8_t *pu8Data, uint_t uTimes)
{
    assert(0);
    return FALSE;
}
